mui.init({
	gestureConfig: {
		doubletap: true
	},
	swipeBack: false,
	keyEventBind: {
		backbutton: false //关闭back按键监听
	},
	statusBarBackground: '#fff'
});
var subpages = [
	{id: 'tab-home.html', href: '../Home/tab-home.html'},	
	{id: 'nurSchInfo.html', href: '../WKPlatformPages/nurSch/nurSchInfo.html'},
	{id: 'workplatform.html', href: 'workplatform.html'},
	{id: 'announceList.html', href: '../HomePages/announceList.html'},
	{id: 'user.html', href: 'user.html', style: {
		top: '0',
		bottom: '51px',
		zIndex:999
	}}
];
var subpage_style = {
	top: '44px',
	bottom: '51px'
};
var tokenTimer = undefined

function plusReadySplash(a) {
	if (window.plus) {
		if(window.plus.isH5) {
			// H5环境直接执行
			a()
		} else {
			// 原生环境等待plus ready
			a()
		}
	} else {
		document.addEventListener('plusready', a, false)
	}
}

function showSplashScreen(imageUrl) {
	plus.navigator.closeSplashscreen(); //干掉启动页
	//声明  
	var view = new plus.nativeObj.View("Splashscreen", {
		top: 0,
		left: 0,
		bottom: 0,
		width: plus.screen.resolutionWidth,
		height: plus.screen.resolutionHeight
	});

	//添加图片  
	if (localStorage.getItem("SplashscreenSrc")) {
		view.drawBitmap(localStorage.getItem("SplashscreenSrc"), {}, {}, "");
	} else {
		view.drawBitmap(imageUrl, {}, {}, "");
	}
	//显示  
	view.show();
	setTimeout(function() {
		if (view) {
			view.close(); //干掉启动广告页  
		}
	}, 1500)
}

var aniShow = {};
document.addEventListener("plusready", onPlusReady, false);

function onPlusReady() {
	// document.addEventListener("netchange", onNetChange, false);
}
mui.plusReady(function() {
	document.addEventListener("netchange", onNetChange, false);
	
	// console.log(screen.width+' '+screen.height)
	var loginInfo = app.getState()
	// var loginInfo = {"IsAdvanced":"Y","WardList":"[{'WardId':'2','WardDesc':'内分泌科护理单元'},{'WardId':'3','WardDesc':'泌尿外科护理单元'}]",
	// 		"loginCode":"demo","loginHireDuty":"","loginID":"0","loginName":"超级管理员",
	// 		"loginPerID":"0","loginRoleCodes":"0","loginWardDesc":"内分泌科护理单元","loginWardId":"2","topRoleDesc":"科护士长"}
	//localStorage.setItem('pinfo',JSON.stringify(loginInfo));

	// setupTabbar();
	if (JSON.stringify(loginInfo) == '{}') {
		var tabbar = document.getElementById('tabbar');
		tabbar.style.display = 'none';
		var header = document.getElementById('header');
		header.style.display = 'none';
		var self = plus.webview.currentWebview();
		var innerLogin = plus.webview.create('login.html', "login", {
			position: 'fixed',
			top: '0',
			left: '0',
			width: '100%',
			height: '100%',
			zIndex: 999999,
			background: '#fff'
		});
		innerLogin.addEventListener('close', function() {
			tabbar.style.display = 'block';
			header.style.display = 'block';
			setupTabbar();
		});
		self.append(innerLogin);
		// 确保登录页面显示
		innerLogin.show();
		return
	} else {
		jQuery('#user').text(loginInfo.loginName.slice(-2));
		jQuery('#loc').text(loginInfo.loginWardDesc);
		setupTabbar();
	}

	var backButtonPress = 0;
	mui.back = function(event) {
		backButtonPress++;
		if (backButtonPress > 1) {
			plus.runtime.quit();
		} else {
			plus.nativeUI.toast('再按一次退出应用');
		}
		setTimeout(function() {
			backButtonPress = 0;
		}, 1000);
		return false;
	};
	//更新状态栏
	window.addEventListener('toNews', function(event) {
		var btn = document.getElementById('../HomePages/announceList.html')
		mui.trigger(btn, 'tap')
		var oldActive = mui('#tabbar .mui-active')[0]
		if (oldActive.id != '../HomePages/announceList.html') {
			oldActive.classList.remove('mui-active')
			btn.classList.add('mui-active')
		}
	});
	// tokenTimer = setTimeout(function() {
	// 	tokenTimer = undefined
	// }, timeInt);
});

/*
 * 设置tabbar
 */
function setupTabbar() {
	var self = plus.webview.currentWebview()
	
	// 创建子页面
	for (var i = 0; i < subpages.length; i++) {
		var sub = plus.webview.create(subpages[i].href, subpages[i].id, subpage_style)
		
		// 将第一个页面显示，其他隐藏
		if (i > 0) {
			sub.hide()
		} else {
			// 确保第一个页面显示
			sub.show()
			var temp = {}
			temp[subpages[i].id] = "true"
			mui.extend(aniShow, temp)
		}
		self.append(sub)
	}

	//当前激活选项
	var activeId = subpages[0].id
	var title = document.getElementById("title")
	// 初始化头部显示
	var header = document.getElementById('header')
	header.style.display = 'block'
	//选项卡点击事件
	mui('.mui-bar-tab').on('tap', 'a', function(e) {
		var targetId = this.getAttribute('id')
		if (targetId == activeId) {
			return
		}

		// 更新标题
		title.innerText = this.querySelector(".tabLabel").innerText
		
		if (targetId == 'user.html') {
			header.style.display = 'none'
		} else {
			header.style.display = 'block'
		}

		// 切换页面
		var targetView = plus.webview.getWebviewById(targetId)
		if(targetView) {
			targetView.show()
		}

		// 隐藏当前页面
		var currentView = plus.webview.getWebviewById(activeId)
		if(currentView) {
			currentView.hide()
		}

		//更改当前活跃的选项卡
		activeId = targetId
	})
}

document.addEventListener('reloadLocName', function(e) {
	var user = app.getState();
	var loc = document.getElementById("loc");
	loc.innerText = user.loginWardDesc
});